package com.lixin.admin.server.config;

import com.google.common.base.Predicates;
import com.lixin.admin.server.anno.SwaggerAnnotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);

        Contact contact = new Contact(
                "物联网", // 文档发布者名称
                "http://www.baidu.com", // 文档发布者的网址
                "505727155@qq.com" // 文档发布者邮箱
        );
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("物联网开发文档") // 文档标题
                .description("物联网开发文档描述信息") // 文档描述
                .version("1.0") // 文档版本
                .contact(contact).build();
        docket.apiInfo(apiInfo);

        docket.select()
                // 标有SwaggerAnnotation注解的方法不生成文档
                .apis(Predicates.not(RequestHandlerSelectors.withMethodAnnotation(SwaggerAnnotation.class)))
                // 设置扫描的包路径
                .apis(RequestHandlerSelectors.basePackage("com.lixin.admin.server.controller"))
                // 以正则表达式来约束生成文档的路径
                .paths(
                        Predicates.or(
                                PathSelectors.regex("/.*"),
                                PathSelectors.regex("/user/.*"),
                                PathSelectors.regex("/goods/.*")
                        )
                )
                .build();

        return docket;
    }
}
